﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("265010")]
    public partial class Area : WebFactory.PageBase
    {
        private readonly MasterDataManage<WinnerCMS.Model.Area> _myBll = new MasterDataManage<WinnerCMS.Model.Area>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblPath.Text = "控制面板 → <a href='Area.aspx'>地区管理</a>";

                BindListBox(null, this.lblArea1);
            }
        }

        #region 网页属性

        /// <summary>
        ///     当前选择的ID
        /// </summary>
        private string SelectID
        {
            set { ViewState["SelectID"] = value; }
            get { return ViewState["SelectID"] == null ? null : ViewState["SelectID"].ToString(); }
        }

        /// <summary>
        ///     当前选择的Name
        /// </summary>
        private string SelectName
        {
            set { ViewState["SelectName"] = value; }
            get { return ViewState["SelectName"] == null ? string.Empty : ViewState["SelectName"].ToString(); }
        }

        /// <summary>
        ///     地区级别
        /// </summary>
        private int LevelArea
        {
            set { ViewState["LevelArea"] = value; }
            get { return ViewState["LevelArea"] == null ? 0 : int.Parse(ViewState["LevelArea"].ToString()); }
        }

        private bool IsEdit
        {
            set { ViewState["IsEdit"] = value; }
            get { return ViewState["IsEdit"] != null && bool.Parse(ViewState["IsEdit"].ToString()); }
        }

        #endregion

        protected void lblArea1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lblArea3.Items.Clear();
            BindListBox(this.lblArea1.SelectedValue, this.lblArea2);

            SelectID = this.lblArea1.SelectedValue;
            SelectName = this.lblArea1.SelectedItem.Text;
            LevelArea = 2;
        }

        protected void lblArea2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindListBox(this.lblArea2.SelectedValue, this.lblArea3);


            SelectID = this.lblArea2.SelectedValue;
            SelectName = this.lblArea2.SelectedItem.Text;
            LevelArea = 3;
        }

        protected void lblArea3_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectID = this.lblArea3.SelectedValue;
            SelectName = this.lblArea3.SelectedItem.Text;
            LevelArea = 3;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void btnEditThis_Click(object sender, EventArgs e)
        {
            this.txtName.Text = SelectName;

            IsEdit = true;
            this.btnOK.Text = "编辑";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        #region 添加或修改

        /// <summary>
        ///     添加或修改
        /// </summary>
        private void Add()
        {
            var model = new WinnerCMS.Model.Area();
            if (IsEdit == false)
            {
                model.Code = SelectID;
                model.Level = LevelArea;
            }
            else
            {
                model.Code = string.Empty;
            }
            model.Name = this.txtName.Text.Trim();
            if (this.cbkIsOneArea.Checked)
            {
                model.ParentCode = null;
            }
            else if (this.cbkIsOneArea.Checked == false && !SelectID.IsNullOrEmpty())
            {
                model.ParentCode = SelectID;
            }

            var resu = this._myBll.Add(model);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
            else
            {
                this.txtName.Text = string.Empty;
                SelectID = null;
                SelectName = "";
                LevelArea = 0;
                IsEdit = false;
                this.btnOK.Text = "添加";
            }
        }

        #endregion

        /// <summary>
        ///     取得下级地区
        /// </summary>
        /// <param name="parentCode"></param>
        /// <param name="lb"></param>
        /// <returns></returns>
        private void BindListBox(string parentCode, ListControl lb)
        {
            var list = this._myBll.GetData(parentCode);
            lb.DataSource = list;
            lb.DataTextField = "Name";
            lb.DataValueField = "Code";
            lb.DataBind();
        }
    }
}